package com.hiwan.unions.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by llg on 2016/4/15.
 * json简单处理
 */
public class JsonUtil {

    //String
    public static String getString(JSONObject context, String ss) {
        try {
            if (context.has(ss)) {
                if (!context.isNull("") || context.isNull(null)) {
                    if (!context.getString(ss).equals("null") && context.getString(ss) != "" && context.getString(ss) != "****") {
                        return context.getString(ss);
                    }
                    return "";
                }
                return "";
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

    //String
    public static int getInt(JSONObject context, String ss) {
        try {
            if (context.has(ss)) {
                if (!context.isNull("") || context.isNull(null)) {
                    if (context.getString(ss) != null && !context.getString(ss).equals("") && !context.getString(ss).equals("null")) {
                        return context.getInt(ss);
                    } else {
                        return 0;
                    }
                }
                return 0;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //String
    public static int getInta(JSONObject context, String ss) {
        try {
            if (context.has(ss)) {
                if (!context.isNull("") || context.isNull(null)) {
                    if (context.getString(ss) != null && !context.getString(ss).equals("") && !context.getString(ss).equals("null")) {
                        return context.getInt(ss);
                    } else {
                        return 1;
                    }
                }
                return 1;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 1;
    }

    //String
    public static boolean getBoolean(JSONObject context, String ss) {
        try {
            if (context.has(ss)) {
                if (!context.isNull("") || context.isNull(null)) {
                    if (context.getString(ss) != null && !context.getString(ss).equals("") && !context.getString(ss).equals("null")) {
                        return context.getBoolean(ss);
                    } else {
                        return false;
                    }
                }
                return false;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static JSONArray getJSONArray(JSONObject context, String ss) {
        JSONArray jsonArray = new JSONArray();
        try {
            if (context != null && context.has(ss)) {
                if (!context.isNull("") || context.isNull(null)) {
                    if (context.getString(ss) != null && context.getString(ss) != "" && context.getString(ss) != "xx" && context.getString(ss) != "{}")
                        jsonArray = context.getJSONArray(ss);
                    return jsonArray;
                }
                return jsonArray;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    public static JSONArray getJSONArray(String ss) {
        JSONArray jsonArray = new JSONArray();
        try {
            if (!CheckUtils.isEmpty(ss)) {
                return new JSONArray(ss);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    public static JSONObject getJSONObject(JSONObject context, String ss) {
        JSONObject jsonObject = new JSONObject();
        try {
            if (context.has(ss)) {
                if (!context.isNull("") || context.isNull(null)) {
                    if (!context.getString(ss).equals("null") && context.getString(ss) != "" && context.getString(ss) != "****" & !context.getString(ss).equals("{}")) {
                        jsonObject = context.getJSONObject(ss);
                        return jsonObject;
                    } else {
                        return jsonObject;
                    }
                }
                return jsonObject;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    public static JSONObject getJSONObject(JSONArray jsonArray, int i) {
        try {
            return jsonArray.getJSONObject(i);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }
}
